﻿@model TopicModel
@if (Model.IsPasswordProtected)
{
    <script asp-location="Footer">
        $(function() {
            $('#button-password-@Model.Id').click(function() {
                var postData = {
                    id: $("#@Html.IdFor(model => model.Id)").val(),
                    password: $('#password-@Model.Id').val()
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("TopicAuthenticate")',
                    data: postData,
                    dataType: 'json',
                    success: function(data) {
                        if (data.Authenticated) {
                            $('#ph-topic-@Model.Id #ph-title-@Model.Id h2').html(data.Title);
                            if ($('#ph-topic-@Model.Id #ph-title-@Model.Id h2').text().length == 0) {
                                $('#ph-title-@Model.Id').hide();
                            }
                            $('#ph-topic-@Model.Id .topic-block-body').html(data.Body);
                            $('#ph-password-@Model.Id').hide();
                            $('#ph-topic-@Model.Id').show();
                            $.validator.unobtrusive.parse('#ph-topic-@Model.Id');
                        } else {
                            $('#password-error-@Model.Id').text(data.Error);
                            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
                        }
                    }
                });
                return false;
            });
        });

        $(document).ready(function() {
            $('#ph-topic-@Model.Id').hide();
            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
        });
    </script>
    <div class="topic-password" id="ph-password-@Model.Id">
        <form asp-route="TopicAuthenticate" method="post">
            <input asp-for="Id" type="hidden" />
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { id = "password-" + @Model.Id, @class = "form-control mt-1" })
                <input type="submit" id="button-password-@Model.Id" value="@T("Topic.Button")" class="btn btn-secondary topic-password-button mt-1 mb-1" />
            </div>
            <div class="password-error">
                <span id="password-error-@Model.Id"></span>
            </div>
        </form>
    </div>


    <div class="topic-block" id="ph-topic-@Model.Id">
        <div class="topic-block-title" id="ph-title-@Model.Id">
            <h2>@Model.Title</h2>
        </div>
        <div class="topic-block-body col-12">
            @Html.Raw(Model.Body)
        </div>
    </div>
}
else
{
    <div class="generalMarginSupporter">
        @if (!String.IsNullOrEmpty(Model.Title))
        {
            <h2 class="generalTitle text-sm-center">@Model.Title</h2>
        }
        <div class="topic-block-body mpt-3 pt-3">
            @Html.Raw(Model.Body)
        </div>
    </div>
}